import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/index.vue'
import Category from '../views/Category/index.vue'
import Cart from '../views/Cart/index.vue'
import Me from '../views/Me/index.vue'
import Search from '../components/Search/index.vue'
import Login from '../components/login/Login.vue'
import Set from '../views/Me/base/Set.vue'
import UserAddress from '../views/Me/base/UserAddress.vue'
import addressEdit from '../views/Me/base/addressEdit.vue'
import AddAddress from '../views/Me/base/AddAddress.vue'
import GoodsDetail from '../components/goods/GoodsDetail.vue'
import OrderConfirm from '../components/goods/OrderConfirm.vue'
import Payment from '../components/goods/Payment.vue'
import PaymentCompleted from '../components/goods/PaymentCompleted.vue'
import MyOrder from '../views/Me/base/MyOrder.vue'
import Register from '../components/login/Register.vue'
import SearchItems from '../components/Search/SearchItems.vue'
import OrderSearch from '../components/Search/OrderSearch.vue'
import OrderSearchItems from '../components/Search/OrderSearchItems.vue'
Vue.use(VueRouter)

const routes = [
  // 重定向到主页
  {
    path: '/',
    redirect: '/Home'
  },
  // 主页
  {
    path: '/Home',
    name: 'Home',
    component: Home
  },
  // 分类页
  {
    path: '/Category',
    name: 'Category',
    component: Category
  },
  // 购物车
  {
    path: '/Cart',
    name: 'Cart',
    component: Cart
  },
  // 我的
  {
    path: '/Me',
    name: 'Me',
    component: Me,
    meta: {
      auth: true
    }
  },
  // 登录页
  {
    path: '/Login',
    name: 'Login',
    component: Login
  },
  // 注册页
  {
    path: '/Register',
    name: 'Register',
    component: Register
  },
  // 主页搜索
  {
    path: '/Search',
    name: 'Search',
    component: Search
  },
  // 主页搜索结果页
  {
    path: '/SearchItems',
    name: 'SearchItems',
    component: SearchItems
  },
  // 订单搜索
  {
    path: '/OrderSearch',
    name: 'OrderSearch',
    component: OrderSearch
  },
  // 订单页搜索结果页
  {
    path: '/OrderSearchItems',
    name: 'OrderSearchItems',
    component: OrderSearchItems
  },
  // 设置
  {
    path: '/Set',
    name: 'Set',
    component: Set
  },
  // 设置/地址
  {
    path: '/Set/UserAddress',
    name: 'UserAddress',
    component: UserAddress
  },
  // 设置/地址/编辑
  {
    path: '/Set/UserAdress/edit',
    name: 'addressEdit',
    component: addressEdit
  },
  // 设置/地址/新增地址
  {
    path: '/Set/UserAdress/add',
    name: 'AddAddress',
    component: AddAddress
  },
  // 商品详情页
  {
    path: '/GoodsDetail/:goodsID',
    name: 'GoodsDetail',
    component: GoodsDetail
  },
  // 订单确认页
  {
    path: '/OrderConfirm',
    name: 'OrderConfirm',
    component: OrderConfirm
  },
  // 支付页
  {
    path: '/Payment',
    name: 'Payment',
    component: Payment
  },
  // 支付完成页
  {
    path: '/PaymentCompleted',
    name: 'PaymentCompleted',
    component: PaymentCompleted
  },
  // 我的订单页
  {
    path: '/MyOrder',
    name: 'MyOrder',
    component: MyOrder
  }
]

const router = new VueRouter({
  routes,
  mode: "history" // 去掉地址栏里的#号键
})
// 全局守卫
router.beforeEach((to, from, next) => {
  if (to.meta.auth) {
    if (window.localStorage.getItem('loginToken')) {
      next()
    } else {
      next({
        name: 'Login'
      })
    }
  } else {
    next()
  }
})
export default router
